package com.tenton.controller;

import com.tenton.MyException.DataExist;
import com.tenton.MyException.DataNotExist;
import com.tenton.pojo.Administrator;
import com.tenton.pojo.Employees;
import com.tenton.pojo.Stock;
import com.tenton.service.AdministratorService;
import com.tenton.service.EmployeesService;
import com.tenton.service.StockService;
import com.tenton.utils.Constants;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("管理员管理")
@RequestMapping("/admin")
public class AdministratorController {
    private Logger logger = Logger.getLogger(AdministratorController.class);
    @Autowired
    AdministratorService administratorService;
    @Autowired
    EmployeesService employeesService;
    @Autowired
    StockService stockService;

    /**
     * 管理员登录界面
     *
     * @return
     */
    @ApiOperation("管理员登录界面")
    @GetMapping("/login")
    public String login() {
        logger.debug("LoginController welcome AppInfoSystem develpor==================");
        return "admin/adminLogin";
    }

    /**
     * 登录验证
     *
     * @param adminName
     * @param pwd
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("登录验证")
    @PostMapping(value = "/doLogin")
    public String doLogin(@ApiParam("管理员名称") @RequestParam("adminName") String adminName,
                          @ApiParam("密码") @RequestParam("pwd") String pwd,
                          HttpSession session, HttpServletRequest request) {
        boolean flag  = administratorService.getLogin(adminName, pwd, session);
        if (flag) {
            return "redirect:/admin/flatform/main";
        } else {
            request.setAttribute("error", "账号或密码不正确！");
            return "admin/adminLogin";
        }
    }

    @GetMapping(value = "/flatform/main")
    public String main(HttpSession session, HttpServletRequest request) {
        //验证是否有session信息，防止非法登录，没有就跳转到登录页面
        Object attr = session.getAttribute(Constants.ADMIN_SESSION);
        if (attr == null) {
            return "/admin/adminLogin";
        }
        List<Stock> stock1 = stockService.StockWithData();
        List<Stock> stock2 = stockService.StockWithNum();
        if (!stock1.isEmpty() || !stock2.isEmpty()) {
            request.setAttribute("msg", "存在预警信息请及时处理");
        } else {
            request.setAttribute("msg", "");
        }
        return "admin/main";
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @ApiOperation("退出登录")
    @GetMapping(value = "/logout")
    public String logout(HttpSession session) {
        //session失效
        session.invalidate();
        return "admin/adminLogin";

    }

    /**
     * 通过Id获取管理员信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("通过Id获取管理员信息")
    @GetMapping("/getAdmin")
    public String getAdmin(Integer id, Model model) {
        administratorService.getAdminById(id, model);
        return "admin/getAdmin";

    }

    /**
     * 跳转到修改个人信息页面
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("跳转到修改个人信息页面")
    @GetMapping("/toUpdate")
    public String editAdministrator(Integer id, Model model) {
        administratorService.getAdminById(id, model);
        return "admin/editAdmin";
    }

    /**
     * 修改
     *
     * @param administrator
     * @param model
     * @param request
     * @return
     */
    @PostMapping("/update")
    public String update(Administrator administrator,Model model, HttpServletRequest request){
        try {
            administratorService.update(administrator,model);
            return "redirect:/admin/login";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "员工信息为空！");
            return "redirect:/admin/toUpdate";
        }
    }

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAdd() {
        return "admin/employees/addEmployee";

    }

    /**
     * 先判断数据库有没有，有就更新，没有就新增
     *
     * @param employee
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("新增员工")
    @PostMapping("/insertEmployees")
    public String insertEmployees(Employees employee,HttpSession session, HttpServletRequest request) {
        try {
            employeesService.insert(employee,session);
            return "redirect:/admin/getAll";
        } catch (DataExist dataExist) {
            request.setAttribute("msg", "此员工信息已存在！");
            return "admin/employees/addEmployee";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "员工信息为空！");
            return "admin/employees/addEmployee";
        }
    }

    /**
     * 跳转到修改员工信息页面
     *
     * @param employee
     * @param model
     * @return
     */
    @ApiOperation("跳转到修改员工信息页面")
    @GetMapping("/toUpdateEmployee")
    public String editEmployees(Employees employee, Model model) {
        employeesService.getEmployeesById(employee.getId(), model);
        return "/admin/employees/editEmployee";
    }

    /**
     * 修改员工信息
     *
     * @param employee
     * @param session
     * @param model
     * @param request
     * @return
     */
    @ApiOperation("修改员工信息")
    @PostMapping("/updateEmployees")
    public String updateEmployees(Employees employee,HttpSession session, Model model,HttpServletRequest request) {
        try {
            employeesService.update(employee,session);
            employeesService.getEmployeesById(employee.getId(), model);
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "员工信息为空！");
            return "/admin/employees/editEmployee";
        }
        return "redirect:/admin/getAll";
    }

    @ApiOperation("通过Id删除员工信息")
    @GetMapping("/deleteEmployees")
    public String deleteEmployees(int empId) {
        employeesService.deleteByPrimaryKey(empId);
        return "redirect:/admin/getAll";
    }

    /**
     * 获取所有员工信息
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有员工信息")
    @GetMapping("/getAll")
    public String getAll(ModelMap model,
                         @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        employeesService.getAll(model, pn);
        return "admin/employees/getEmployeesList";
    }

    /**
     * 员工姓名模糊查询
     *
     * @param empName
     * @param pn
     * @param model
     * @return
     */
    @ApiOperation("员工姓名模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(@ApiParam("员工姓名") @RequestParam(value = "empName", required = false) String empName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn,
                              ModelMap model
    ) {
        employeesService.getByParams(empName, pn, model);
        return "admin/employees/getEmployeesByParams";
    }
}
